package com.demo.project_template.util;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.Scanner;

@Slf4j
public class ReadBook {

    public static void main(String[] args) throws IOException {
        //选择小说的地址
        String url = "http://www.du00.co/read/115/115988/41180277_2.html";

        String topUrl = "http://www.du00.co";
        boolean flag = true;
        while (flag){
            Connection connect= Jsoup.connect(url);
            //这里的header里的参数是为了伪装成浏览器，对于某些有防爬虫的网站必须伪装成浏览器才能进行爬取，这里的参数可以当做常量来使用
            Document document = connect.header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36").get();
            String h1 = document.select("h1").text();
            log.info(h1);
            for (Element child : document.getElementsByTag("p")) {
                log.info(child.text());
            }

            log.info("请输入指令:top=上一章，quit=退出观看，其他=下一章");
            Scanner in = new Scanner(System.in);
            String input = in.next();
            //上一章
            if ("top".equals(input)) {
                url = topUrl+document.selectFirst("#prev_url").attr("href");
            }else if ("quit".equals(input)){
                flag = false;
            }
            url = topUrl+document.selectFirst("#next_url").attr("href");
        }
        log.info("最终章节:"+url);
    }
}
